/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.mc.manager.impl;

import com.xy.biz.mc.dao.MessageDao;
import com.xy.biz.mc.dao.MessageDataDao;
import com.xy.biz.mc.domain.bo.MessageBO;
import com.xy.biz.mc.domain.converter.MessageConverter;
import com.xy.biz.mc.domain.po.MessageDataPO;
import com.xy.biz.mc.domain.query.MessageQuery;
import com.xy.biz.mc.domain.req.MultipleMessageRequest;
import com.xy.biz.mc.domain.resp.GetMessageResponse;
import com.xy.biz.mc.manager.MessageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 *
 *
 * @author zsp
 * @date 2021-2-25
 */
@Service
public class MessageManagerImpl implements MessageManager {

	@Autowired
	private MessageDao messageDao;
	@Autowired
	private MessageDataDao messageDataDao;

	@Transactional
	@Override
	public int saveMessage(MessageBO messageBO) {
		int affectedRows = messageDao.saveMessage(MessageConverter.toPO(messageBO));
		if(affectedRows > 0) {
			messageDataDao.saveMessageData(MessageDataPO.builder()
					.messageId(messageBO.getMessageId())
					.data(messageBO.getData())
					.build());
		}
		return affectedRows;
	}

	@Transactional
	@Override
	public int saveAllMessage(Collection<MessageBO> collection) {
		int affectedRows = messageDao.saveAllMessage(MessageConverter.toPO(collection));
		if(affectedRows > 0) {
			List<MessageDataPO> messageDataPOList = new ArrayList<>();
			for(MessageBO messageBO : collection) {
				messageDataPOList.add(MessageDataPO.builder()
						.messageId(messageBO.getMessageId())
						.data(messageBO.getData())
						.build());
			}
			messageDataDao.saveAllMessageData(messageDataPOList);
		}
		return affectedRows;
	}

	@Override
	public int updateMessage(MessageBO messageBO) {
		return messageDao.updateMessage(MessageConverter.toPO(messageBO));
	}

	@Override
	public int deleteMessage(Long messageId) {
		return messageDao.deleteMessage(messageId);
	}

	@Override
	public MessageBO getMessage(Long messageId) {
		return MessageConverter.fromPO(messageDao.getMessage(messageId));
	}

	@Override
	public List<MessageBO> listMessageById(Collection<Long> collection) {
		return MessageConverter.fromPO(messageDao.listMessageById(collection));
	}

	@Override
	public List<GetMessageResponse> listMessage(MessageQuery query) {
		List<MessageBO> messageBOS = MessageConverter.fromPO(messageDao.listMessage(query));
		Set<Long> messageIds = messageBOS.stream().map(e -> e.getMessageId()).collect(Collectors.toSet());
		List<MessageDataPO> messageDataPOS = messageDataDao.listMessageDataById(messageIds);
		Map<Long, MessageDataPO> collect = messageDataPOS.stream().collect(Collectors.toMap(e -> e.getMessageId(), e -> e));
		List<GetMessageResponse> getMessageResponses = MessageConverter.toResponse(messageBOS);
		getMessageResponses.stream().forEach(e -> {
			MessageDataPO messageDataPO = collect.get(e.getMessageId());
			if (Objects.nonNull(messageDataPO)) {
				e.setData(new String(messageDataPO.getData()));
			}
		});
		return getMessageResponses;
	}

	@Override
	public int countMessage(MessageQuery query) {
		return messageDao.countMessage(query);
	}

	@Override
	public int deleteMessageMultiple(MultipleMessageRequest request) {
		return messageDao.deleteMessageMultiple(request);
	}

	@Override
	public int haveReadMultiple(MultipleMessageRequest request) {
		return messageDao.haveReadMultiple(request);
	}

}
